/*
 * frmServidor.java
 *
 * Created on 11 de Junho de 2007, 19:59
 */

package servidor;

import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author  tschimuneck
 */
public class frmServidor extends javax.swing.JFrame implements Runnable
{
    private Servidor server;
    /** Creates new form frmServidor */
    public frmServidor()
    {
        initComponents();
        this.server = new Servidor();
        this.lstUsuarios.setModel(new DefaultListModel());
        this.addUser("bohrer","bohrer");
        this.addUser("thiago","thiago");
    }
    
    private void addUser(String name,String passwd)
    {
        this.server.addUser(name,passwd);
        this.atualizaLista();
    }
    
    private void atualizaLista()
    {
        DefaultListModel dlm = (DefaultListModel)this.lstUsuarios.getModel();
        dlm.removeAllElements();
        for(int x=0;x<this.server.getUsers().length;x++)
        {
            dlm.addElement(this.server.getUsers()[x]);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Codigo Gerado ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        btnAtivDesvativ = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        tfConexoes = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstUsuarios = new javax.swing.JList();
        lbUsuario = new javax.swing.JLabel();
        tfUsuario = new javax.swing.JTextField();
        lbSenha = new javax.swing.JLabel();
        pfSenha = new javax.swing.JPasswordField();
        lbConfirma = new javax.swing.JLabel();
        pfConfirma = new javax.swing.JPasswordField();
        btnAdicionarUsuario = new javax.swing.JButton();
        btnExcluirUsuario = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        btnAtivDesvativ.setText("Ativar Servidor");
        btnAtivDesvativ.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnAtivDesvativActionPerformed(evt);
            }
        });

        jLabel1.setText("Conex\u00f5es ativas:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Usu\u00e1rios"));
        lstUsuarios.addListSelectionListener(new javax.swing.event.ListSelectionListener()
        {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt)
            {
                lstUsuariosValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(lstUsuarios);

        lbUsuario.setLabelFor(tfUsuario);
        lbUsuario.setText("Usu\u00e1rio:");

        lbSenha.setLabelFor(pfSenha);
        lbSenha.setText("Senha:");

        lbConfirma.setLabelFor(pfConfirma);
        lbConfirma.setText("Confirma:");

        btnAdicionarUsuario.setText("Adicionar");
        btnAdicionarUsuario.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnAdicionarUsuarioActionPerformed(evt);
            }
        });

        btnExcluirUsuario.setText("Excluir");
        btnExcluirUsuario.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnExcluirUsuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAdicionarUsuario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                        .addComponent(btnExcluirUsuario)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbUsuario)
                            .addComponent(lbSenha)
                            .addComponent(lbConfirma))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pfConfirma)
                            .addComponent(pfSenha, 0, 0, Short.MAX_VALUE)
                            .addComponent(tfUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
                        .addGap(17, 17, 17))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbUsuario)
                            .addComponent(tfUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbSenha)
                            .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbConfirma)
                            .addComponent(pfConfirma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAdicionarUsuario)
                            .addComponent(btnExcluirUsuario)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfConexoes, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                        .addComponent(btnAtivDesvativ)
                        .addGap(26, 26, 26))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(tfConexoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnAtivDesvativ))
                .addGap(21, 21, 21)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnExcluirUsuarioActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnExcluirUsuarioActionPerformed
    {//GEN-HEADEREND:event_btnExcluirUsuarioActionPerformed
// TODO adicione seu codigo de manipulacao aqui:
        int x;
        if ((x = this.lstUsuarios.getSelectedIndex()) > -1)
        {
            String name = (String) this.lstUsuarios.getSelectedValue();
            this.server.delUser(name);
            this.atualizaLista();
            this.tfUsuario.setText("");
            this.pfConfirma.setText("");
            this.pfSenha.setText("");
            this.tfUsuario.requestFocus();
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Selecione um usuario","Erro",JOptionPane.ERROR_MESSAGE);
            this.lstUsuarios.requestFocus();
        }
    }//GEN-LAST:event_btnExcluirUsuarioActionPerformed
    
    private void lstUsuariosValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_lstUsuariosValueChanged
    {//GEN-HEADEREND:event_lstUsuariosValueChanged
// TODO adicione seu codigo de manipulacao aqui:
        if (this.lstUsuarios.getSelectedIndex() > -1)
        {
            this.tfUsuario.setText((String)this.lstUsuarios.getSelectedValue());
        }
    }//GEN-LAST:event_lstUsuariosValueChanged
    
    private void btnAdicionarUsuarioActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnAdicionarUsuarioActionPerformed
    {//GEN-HEADEREND:event_btnAdicionarUsuarioActionPerformed
// TODO adicione seu codigo de manipulacao aqui:
        int x;
        String senha = new String(this.pfSenha.getPassword());
        String confirma = new String(this.pfConfirma.getPassword());
        if (!senha.equals(confirma))
        {
            JOptionPane.showMessageDialog(this,"Senha nao confere!","Erro",JOptionPane.ERROR_MESSAGE);
            this.pfSenha.requestFocus();
        }
        else
        {
            this.server.addUser(this.tfUsuario.getText(),new String(this.pfSenha.getPassword()));
            this.atualizaLista();
            this.tfUsuario.setText("");
            this.pfConfirma.setText("");
            this.pfSenha.setText("");
            this.tfUsuario.requestFocus();
        }
        
    }//GEN-LAST:event_btnAdicionarUsuarioActionPerformed
    
    private void btnAtivDesvativActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnAtivDesvativActionPerformed
    {//GEN-HEADEREND:event_btnAtivDesvativActionPerformed
// TODO adicione seu codigo de manipulacao aqui:
        if (this.server.isActive())
        {
            this.server.down();
            this.server = new Servidor();
            this.btnAtivDesvativ.setText("Ativar Servidor");
        }
        else
        {
            this.server.up();
            this.btnAtivDesvativ.setText("Parar Servidor");
            new Thread(this).start();
        }
    }//GEN-LAST:event_btnAtivDesvativActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new frmServidor().setVisible(true);
            }
        });
    }
    
    public void run()
    {
        while(this.server.isActive())
        {
            this.tfConexoes.setText(new Integer(this.server.getActiveConnections()).toString());
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
    }
    
    // Declaracao de variaveis - nao modifique//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionarUsuario;
    private javax.swing.JToggleButton btnAtivDesvativ;
    private javax.swing.JButton btnExcluirUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbConfirma;
    private javax.swing.JLabel lbSenha;
    private javax.swing.JLabel lbUsuario;
    private javax.swing.JList lstUsuarios;
    private javax.swing.JPasswordField pfConfirma;
    private javax.swing.JPasswordField pfSenha;
    private javax.swing.JTextField tfConexoes;
    private javax.swing.JTextField tfUsuario;
    // Fim da declaracao de variaveis//GEN-END:variables
    
}
